﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class BehaviourMate
{
    public string m_Name;
    public int m_ID;   //  存储parameter Index之类的
    public CommonData m_TargetData;
    public float m_MinValue;
    public float m_MaxValue;
    public BehaviourMate(string name, int id, CommonData data)
    {
        m_Name = name;
        m_ID = id;
        m_TargetData = data;
        m_MinValue = 0;
        m_MaxValue = 1;
    }

    public JsonData Save()
    {
        JsonData root = new JsonData() ;
        root["TargetDataType"] = (int)m_TargetData.m_DataType;
        root["ID"] = m_ID;
        root["MinValue"] = m_MinValue;
        root["MaxValue"] = m_MaxValue;
        switch (m_TargetData.m_DataType)
        {
            case CommonData.DataType.Bool:
                root["TargetData"] = m_TargetData.m_TargetDataBool;
                break;
            case CommonData.DataType.Int:
                root["TargetData"] = m_TargetData.m_TargetDataInt;
                break;
            case CommonData.DataType.Float:
                root["TargetData"] = m_TargetData.m_TargetDataFloat;
                break;
            case CommonData.DataType.String:
                root["TargetData"] = m_TargetData.m_TargetDataString;
                break;
        }
        return root;
    }
    public bool Load(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if (rootObj.ContainsKey("TargetDataType"))
            m_TargetData.m_DataType = (CommonData.DataType)rootObj["TargetDataType"].ValueAsInt();
        if (rootObj.ContainsKey("ID"))
            m_ID = rootObj["ID"].ValueAsInt();
        if (rootObj.ContainsKey("MinValue"))
            m_MinValue = (int)rootObj["MinValue"].ValueAsDouble();
        if (rootObj.ContainsKey("MaxValue"))
            m_MaxValue = (int)rootObj["MaxValue"].ValueAsDouble();

        if (rootObj.ContainsKey("TargetData"))
        {
            switch (m_TargetData.m_DataType)
            {
                case CommonData.DataType.Bool:
                    m_TargetData.m_TargetDataBool = rootObj["TargetData"].ValueAsBoolean();
                    break;
                case CommonData.DataType.Int:
                    m_TargetData.m_TargetDataInt = rootObj["TargetData"].ValueAsInt();
                    break;
                case CommonData.DataType.Float:
                    m_TargetData.m_TargetDataFloat = (float)rootObj["TargetData"].ValueAsDouble();
                    break;
                case CommonData.DataType.String:
                    m_TargetData.m_TargetDataString = rootObj["TargetData"].ValueAsString();
                    break;
            }
        }
        return true;
    }
}
public class ShipEventBehaviours
{
    public enum BehavioursType
    {
        None,
        PartState,
    }
    public BehaviourMate behaviour { get { return m_Behaviour; }set { m_Behaviour = value; } }
    public Ship m_Ship;
    public BehaviourMate m_Behaviour ;
    public BehavioursType m_BehavioursType = BehavioursType.None;

    public virtual void Init()
    {
    }
    public virtual void BindShip(Ship ship)
    {
        if (ship == null)
        {
            Debug.LogWarning("ShipEventTrigger::BindShip(Ship) Cann't found ship in ShipEventTriggerr");
            return;
        }
    }
    public virtual List<string> GetParameterList()
    {
        List<string> parameterList = new List<string>();
        return parameterList;
    }

    public virtual void Execute()
    {

    }
    public virtual JsonData Save()
    {
        JsonData root = new JsonData();
        root["BehavioursType"] = (int)m_BehavioursType;
        root["CommonData"] = m_Behaviour.Save();
        return root;
    }
    public virtual bool Load(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if (!rootObj.ContainsKey("BehavioursType"))
            return false;
        m_BehavioursType = (BehavioursType)rootObj["BehavioursType"].ValueAsInt();

        if (rootObj.ContainsKey("CommonData"))
        {
            m_Behaviour = new BehaviourMate("", -1, new CommonData());
            m_Behaviour.Load(rootObj["CommonData"]);
        }
        return true;
    }
    public static ShipEventBehaviours Create(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        BehavioursType bahavioursType = BehavioursType.None;
        if (rootObj.ContainsKey("BehavioursType"))
            bahavioursType = (BehavioursType)rootObj["BehavioursType"].ValueAsInt();

        ShipEventBehaviours bahaviours = null;
        switch (bahavioursType)
        {
            case BehavioursType.PartState:
                bahaviours = new PartStateBehaviour();
                break;
        }
        bahaviours.Load(root);
        return bahaviours;
    }


}